What is stream-consume?
The 'stream-consume' npm package is used to ensure that a stream is completely consumed. This is particularly useful when working with streams that you need to fully read or process, as it helps to avoid memory leaks by ensuring that all data is read from the stream.
What are stream-consume's main functionalities?
Consume a Readable Stream
This feature allows you to consume a readable stream. The code sample demonstrates creating a readable stream and then consuming it using the 'stream-consume' package.
const consume = require('stream-consume');
const { Readable } = require('stream');
const readable = new Readable({
read(size) {
this.push('data');
this.push(null); // No more data
}
});
consume(readable);
Handle Stream Errors
This feature ensures that any errors emitted by the stream are handled. The code sample demonstrates creating a readable stream that emits an error and then consuming it using the 'stream-consume' package.
const consume = require('stream-consume');
const { Readable } = require('stream');
const readable = new Readable({
read(size) {
this.emit('error', new Error('Stream error'));
}
});
consume(readable);
Other packages similar to stream-consume
stream-to-array
The 'stream-to-array' package converts a stream into an array of its data chunks. Unlike 'stream-consume', which simply ensures the stream is read, 'stream-to-array' collects the data for further processing.
get-stream
The 'get-stream' package is used to get the contents of a stream as a string, buffer, or array. It provides more functionality compared to 'stream-consume' by allowing you to specify the format of the consumed data.
concat-stream
The 'concat-stream' package is used to concatenate the data from a stream and pass it to a callback when the stream ends. It is similar to 'stream-consume' but also provides the concatenated data for further use.
stream-consume
A node module ensures a Readable stream continues flowing if it's not piped to
another destination.
npm install stream-consume
Usage
Simply pass a stream to stream-consume
.
Both legacy streams and streams2 are supported.
var consume = require('stream-consume');
consume(readableStream);
Details
Only Readable streams are processed (as determined by presence of readable
property and a resume
property that is a function). If called with anything
else, it's a NOP.
For a streams2 stream (as determined by presence of a _readableState
property), nothing is done if the stream has already been piped to at least
one other destination.
resume()
is used to cause the stream to continue flowing.
License
The MIT License (MIT)
Copyright (c) 2014 Aron Nopanen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.